class_name DropItem
extends RigidBody2D

@export_category("ItemDropEffect")
@export var min_horizontal_speed: float = 50.0
@export var max_horizontal_speed: float = 80.0
@export var min_vertical_speed: float = 150.0
@export var max_vertical_speed: float = 250.0

@export_category("ItemConfig")
@export var item_config: ItemBaseConfig

var start_velocity_x: float
var start_gravity_scale: float
@export var water_velocity_scale: float = 0.3
@export var water_gravity_scale: float = 0.3

@export var can_push: bool = true

@export_category("Map")
@export var show_map_marker: bool = false
@export var map_marker_scale: float = 0.1

var level: Level = null
@onready var collision_shape_2d: CollisionShape2D = $CollisionShape2D

# frame counter 


func _ready() -> void:
	level = get_tree().current_scene as Level
	name = GlobalUtils.generate_id()
	
	if show_map_marker:
		var level = get_tree().current_scene as Level
		level.game_map.add_marker(name, global_position, item_config.icon_name, map_marker_scale)
		

func _exit_tree() -> void:
	if show_map_marker and is_inside_tree():
		var level = get_tree().current_scene as Level
		if level:
			level.game_map.destroy_marker(name)
	

func _process(delta: float) -> void:
	if show_map_marker:
		var level = get_tree().current_scene as Level
		level.game_map.update_marker_pos(name, global_position)
	
	if GameManager.frame_counter % 2 == 0:
		process_speed_affect()


func start_drop():
	var direction = 1 if randf_range(-1.0, 1.0) >= 0 else -1
	var x_speed = randf_range(min_horizontal_speed, max_horizontal_speed) * direction
	var y_speed = randf_range(min_vertical_speed, max_vertical_speed)
	linear_velocity = Vector2(x_speed, -y_speed)
	angular_velocity = randf_range(-5.0, 5.0)
	remember_start_velocity_gravity()
	

func start_drop_no_fly():
	remember_start_velocity_gravity()
	
	
func remember_start_velocity_gravity():
	start_velocity_x = linear_velocity.x
	start_gravity_scale = gravity_scale
	

func throw(direction: int):
	var x_speed = randf_range(min_horizontal_speed, max_horizontal_speed) * direction
	var y_speed = randf_range(min_vertical_speed, max_vertical_speed)
	linear_velocity = Vector2(x_speed, -y_speed)
	angular_velocity = randf_range(-5.0, 5.0)
	remember_start_velocity_gravity()
	

func on_pickup():
	queue_free()
	# more effect
	
	get_tree().current_scene.emit_particle("particle_pickup_01", global_position)


func enter_water():
	linear_velocity = linear_velocity * water_velocity_scale
	gravity_scale = start_gravity_scale * water_gravity_scale
	
	
func exit_water():
	gravity_scale = start_gravity_scale


func disable_physics():
	if collision_shape_2d:
		collision_shape_2d.disabled = true
	gravity_scale = 0.0
	

func enable_physics():
	if not collision_shape_2d:
		collision_shape_2d = $CollisionShape2D
	
	if collision_shape_2d:
		collision_shape_2d.disabled = false
	gravity_scale = 1.0


func process_speed_affect():
	var is_in_water: bool = false
	for water_rect in level.all_water_rects:
		if water_rect.has_point(global_position):
			is_in_water = true
			break
	
	if is_in_water:
		enter_water()
	else:
		exit_water()
		
		
		
		
